package com.sangeng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sangeng.domain.ResponseResult;
import com.sangeng.dto.RoleDto;
import com.sangeng.entity.Role;
import com.sangeng.entity.RoleMenu;
import com.sangeng.service.IRoleMenuService;
import com.sangeng.service.IRoleService;
import com.sangeng.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/system/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IRoleMenuService roleMenuService;

    // 获取分页列表并传参数
    @GetMapping("/list")
    // Query格式请求参数：pageNum: 页码 pageSize: 每页条数 roleName：角色名称 status：状态
    public ResponseResult list(String roleName, String status, int pageNum, int pageSize) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        // 如果角色名不为空 根据角色名模糊查询
        wrapper.like(StringUtils.hasText(roleName), Role::getRoleName, roleName);
        wrapper.eq(StringUtils.hasText(status), Role::getStatus, status);

        Page<Role> page = new Page<>(pageNum, pageSize);
        Page<Role> page1 = roleService.page(page, wrapper);

        List<Role> roles = page1.getRecords();

        PageVo pageVo = new PageVo(roles, page1.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    // 修改角色的停启用状态
    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody Role role) {
        Role role1 = roleService.getById(role.getId());
        role1.setStatus(role.getStatus());
        return roleService.updateById(role1) ? ResponseResult.okResult() : ResponseResult.errorResult(500, "修改失败");
    }

    // 新增
    @PostMapping
    public ResponseResult add(@RequestBody RoleDto roleDto) {
        Role role = new Role();
        role.setRoleName(roleDto.getRoleName());
        role.setRoleKey(roleDto.getRoleKey());
        role.setRoleSort(roleDto.getRoleSort());
        role.setStatus(roleDto.getStatus());
        role.setRemark(roleDto.getRemark());

        roleService.save(role);

        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getRoleName, roleDto.getRoleName());

        Role oneRole = roleService.getOne(wrapper);
        Arrays.stream(roleDto.getMenuIds()).forEach(menuId -> {
            roleMenuService.save(new RoleMenu(oneRole.getId(), menuId));
        });

        return ResponseResult.okResult();
    }

    // 更新
    @PutMapping
    public ResponseResult update(@RequestBody RoleDto roleDto) {
        Role role = new Role();
        role.setId(roleDto.getId());
        role.setRoleName(roleDto.getRoleName());
        role.setRoleKey(roleDto.getRoleKey());
        role.setRoleSort(roleDto.getRoleSort());
        role.setStatus(roleDto.getStatus());
        role.setRemark(roleDto.getRemark());
        roleService.updateById(role);
        roleMenuService.remove(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, roleDto.getId()));
        Arrays.stream(roleDto.getMenuIds()).forEach(menuId -> {
            roleMenuService.save(new RoleMenu(roleDto.getId(), menuId));
        });
        return ResponseResult.okResult();
    }


    // 获取详情
    @GetMapping("/{id}")
    public ResponseResult getInfo(@PathVariable("id") Long id) {
        Role role = roleService.getById(id);
        return ResponseResult.okResult(role);
    }

    // 删除
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable("id") Long id) {
        roleService.removeById(id);
        return ResponseResult.okResult();
    }

    // 查询所有角色列表
    @GetMapping("/listAllRole")
    public ResponseResult listAllRole() {
        return ResponseResult.okResult(roleService.list());
    }
}
